package com.connor.hozon.bom.common.util.user;


import com.connor.hozon.bom.common.base.constant.SystemStaticConst;
import com.connor.hozon.bom.common.util.node.NodeUtil;
import com.connor.hozon.bom.sys.entity.Tree;
import com.connor.hozon.bom.sys.entity.User;
import com.connor.hozon.bom.sys.service.TreeService;
import org.springframework.security.authentication.encoding.Md5PasswordEncoder;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.context.SecurityContextImpl;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/*
 * 类描述：
 * @auther linzf
 * @create 2017/12/11 0011
 */
public class UserInfo {

    /**
     * 功能描述：加载菜单节点的数据
     *
     * @return
     */
    public static List<Tree> loadUserTree(TreeService treeService) {
        Map<Long, Tree> treeMap = new HashMap<Long, Tree>();
        User user = getUser();
        List<Tree> trees = treeService.loadUserTree(user);
        for (Tree tree : treeService.loadUserTree(user)) {
            treeMap.put(tree.getId(), tree);
        }
        List<Tree> treeList = NodeUtil.getChildNodes(new ArrayList<Tree>(treeMap.values()), 0l);
        return treeList;
    }

    /**
     * 功能描述：实现对密码进行MD5加密
     *
     * @param password
     * @return
     */
    public static String encode(String password) {
        Md5PasswordEncoder encoder = new Md5PasswordEncoder();
        return encoder.encodePassword(password, SystemStaticConst.SALT);
    }

    /**
     * 功能描述：获取当前登陆的用户的信息 注释：强转一个null对象不会产生报错
     *
     * @return
     */
    public static User getUser() {
        RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
        if (null == attributes) {
            return null;
        } else {
            HttpServletRequest request = ((ServletRequestAttributes) attributes).getRequest();
            SecurityContextImpl securityContextImpl = (SecurityContextImpl) request.getSession()
                    .getAttribute("SPRING_SECURITY_CONTEXT");
            return (User) Optional.ofNullable(securityContextImpl.getAuthentication().getPrincipal()).orElse(null);
        }
    }

    /**
     * 功能描述：获取当前登陆的用户的权限集合
     *
     * @return
     */
    public static List<GrantedAuthority> getGrantedAuthority() {
        return (List<GrantedAuthority>) Optional
                .ofNullable(SecurityContextHolder.getContext().getAuthentication().getAuthorities())
                .orElse(new ArrayList<>());
    }

    /**
     * 功能描述：判断当前的用户是否包含某一个权限
     *
     * @param authority 注释： allMatch：Stream 中全部元素符合传入的 predicate，返回 true anyMatch：Stream
     *                  中只要有一个元素符合传入的 predicate，返回 true noneMatch：Stream 中没有一个元素符合传入的
     *                  predicate，返回 true
     * @return
     */
    public static boolean hasAuthority(String authority) {
        return getGrantedAuthority().stream().anyMatch(obj -> obj.getAuthority().equalsIgnoreCase(authority));
    }

}
